package com.example.administrator.myapplication.service;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.example.administrator.myapplication.model.User;

/**
 * Created by Administrator on 2017/7/24 0024.
 */

public class LoginJudge {
    private Context mContext;
    private final static String LANDING_STATUS_KEY = "LANDING_STATUS";
    private SharedPreferences sharedPreferences;
    private final static String account_key = "account_username";

    public LoginJudge(Context context){
        this.mContext = context;
        sharedPreferences = mContext.getSharedPreferences(LANDING_STATUS_KEY, 0);
    }

    /**
     *默认账户会通过合法程序注销
     *
     * 查询处于登录状态的账户且IMEI号码为本机
     * @param dbm
     * @return
     */
    @Deprecated
    public User getLoginStatus(DBManager dbm){
           User user =  dbm.queryAccountLandingStatusIsLogIn(true);
           if(user!=null){

               return user;
           }
            return null;
        }

    public synchronized void setAccountLandingStatus(String username){
        SharedPreferences.Editor e = sharedPreferences.edit();
        e.putString(account_key,username);
        e.commit();
    }
    public String selectAccountLandingStatus(){
      return  sharedPreferences.getString(account_key,null);
    }

    /**
     * 注销账户
     * @param user 需要注销的账户,如果为null则默认为当前保存的账户信息
     * @return true 如果成功注销
     */
    public boolean write_off_accounts(User user){
        if(user == null){
            user = new User();
        }
        setAccountLandingStatus(user.getUsername());
        return true;
    }
}
